// pages/me/ImmediateRenewal.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    is_prohibit: false,
    address: [],
    is_address: false,
    address_id: 0,
    orderDeatils: [],
    box_arr: [],
    box_num: [],
    box_index: 0,
    brand_arr: [],
    brand_num: [],
    brand_index: 0,
    select_arr: [],
    select_num: [],
    select_index: 0,
    select_name: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    });
    if (options.prohibit) {
      this.setData({
        is_prohibit: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取订单信息
    this.getOrderInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取订单信息
  getOrderInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getOrderInfo',
      method: 'GET',
      data: {
        order_id: this.data.id
      },
      success: function (e) {
        console.log(e);
        that.setData({
          orderDeatils: e.data
        });
        // 获取当前盒子的所有价格
        that.getBoxPrice();
      }
    })
  },
  // 获取盒子所有的价位
  getBoxPrice: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxPrice',
      method: 'GET',
      data: {
        boxId: this.data.orderDeatils.box_id
      },
      success: function (e) {
        // console.log(e);
        that.setData({
          select_arr: e.data
        });
        // 设置一下选择时间picker的值
        that.setSelectValue();
      }
    })
  },
  // 设置picker选择月份的值
  setSelectValue: function () {
    var arr = [];
    for (var x in this.data.select_arr) {
      //x = index
      // that.data.imgs.push({ path: temp[tm], label: '添加标签' });
      // console.log(this.data.select_arr[x]['month'], this.data.select_arr[x]['name']);
      arr.push(this.data.select_arr[x]['name']);
      if (this.data.select_arr[x]['month'] == this.data.orderDeatils.sum_box) {
        var price = 'orderDeatils.sum_price';
        this.setData({
          [price]: this.data.select_arr[x]['price'],
          select_index: x,
          select_name: this.data.select_arr[x]['name']
        });
      }
    }
    this.setData({
      select_num: arr
    });
  },
  // 修改月份
  bindMonthChange: function (e) {
    // console.log(e);
    for (var x in this.data.select_arr) {
      if (x = e.detail.value) {
        var price = 'orderDeatils.sum_price';
        var sum_box = 'orderDeatils.sum_box';
        var rest_box = 'orderDeatils.rest_box';
        this.setData({
          [price]: this.data.select_arr[x]['price'],
          [sum_box]: this.data.select_arr[x]['month'],
          [rest_box]: this.data.select_arr[x]['month'],
          select_index: x,
          select_name: this.data.select_arr[x]['name']
        });
        break;
      }
    }
    wx.request({
      url: app.globalData.serverUrl + 'changePrice',
      method: 'GET',
      data: {
        id: this.data.orderDeatils.id,
        sum_box: this.data.orderDeatils.sum_box,
      },
      success: function (e) {
        console.log(e);
      }
    })
    console.log(this.data.orderDeatils);
  },
  // 支付按钮
  // 获取微信支付参数,挑起微信支付
  wxpay: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'wxpay',
      method: 'GET',
      data: {
        openid: app.globalData.openid,
        id: this.data.orderDeatils.id
      },
      success: function (e) {
        // console.log(e.data.appId);
        if (e.data.package) {
          wx.requestPayment({
            timeStamp: e.data.timeStamp,
            nonceStr: e.data.nonceStr,
            package: e.data.package,
            signType: 'MD5',
            paySign: e.data.paySign,
            success(res) {
              // console.log(res);
              if (res.errMsg == 'requestPayment:ok') {
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 800,
                  success: function (res) {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 800);
                  }
                });
              } else {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '支付失败！',
                  confirmColor: '#fc5871'
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '支付失败！',
            confirmColor: '#fc5871'
          })
        }
      }
    })
  }
})